package com.earl.gsm.web.equipment;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.earl.gsm.commons.vo.DwzMessage;
import com.earl.gsm.domain.gsm.equipment.MaintainTable;
import com.earl.gsm.domain.gsm.equipment.RentTable;
import com.earl.gsm.domain.sercurity.user.User;
import com.earl.gsm.service.MaintainTableService;
import com.earl.gsm.web.BaseController;
import com.wordnik.swagger.annotations.ApiOperation;

@Controller
@RequestMapping(value = "/equipment/maintianTable")
public class MaintainTableController extends BaseController {

	private final Logger log = Logger.getLogger(MaintainTableController.class);

	@Autowired
	private MaintainTableService service;

	@RequestMapping(value = "/maintainAllMsg", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(value = "维修表所有信息", notes = "all message of maintainTable", httpMethod = "POST", response = MaintainTable.class, responseContainer = "List")
	public ModelAndView getAllMsg() {
		log.debug("get all message of maintainTable");
		return new ModelAndView("equipment/maintainAllMsg", "maintainList", service.findAll());
	}

	@RequestMapping(value = "/preAdd")
	@ApiOperation(value = "添加维护表", notes = "add new maintainTable", httpMethod = "GET", response = User.class, responseContainer = "List")
	public String preAdd() {
		return "equipment/addMaintainTable";
	}

	@RequestMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(value = "增加维修表", notes = "add new maintainTable", httpMethod = "POST", response = DwzMessage.class)
	public ResponseEntity<DwzMessage> applyForMatch(MaintainTable maintainTable) {
		service.save(maintainTable);
		DwzMessage dwzMessage = new DwzMessage();
		dwzMessage.setStatusCode(200);
		dwzMessage.setMessage("增加成功");
		dwzMessage.setCallbackType("forward");
		dwzMessage.setForwardUrl("/equipment/maintianTable/maintainAllMsg");
		dwzMessage.setNavTabId("维修表所有信息");
		return new ResponseEntity<DwzMessage>(dwzMessage, HttpStatus.OK);
	}

	@RequestMapping(value = "/delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(value = "删除选中维修表", notes = "delete selected maintainTable", httpMethod = "GET", response = DwzMessage.class)
	public ResponseEntity<DwzMessage> allMatchApply(@PathVariable Long id) {
		service.delete(id);
		DwzMessage dwzMessage = new DwzMessage();
		dwzMessage.setStatusCode(200);
		dwzMessage.setCallbackType("forward");
		dwzMessage.setForwardUrl("/equipment/maintianTable/maintainAllMsg");
		return new ResponseEntity<DwzMessage>(dwzMessage, HttpStatus.OK);
	}

	@RequestMapping(value = "/preMaintainUpdate/{id}")
	@ApiOperation(value = "预处理更新赛事信息", notes = "find All User", httpMethod = "GET", response = MaintainTable.class, responseContainer = "List")
	public ModelAndView preUpdate(@PathVariable Long id) {
		return new ModelAndView("equipment/preMaintainUpdate", "maintain", service.findOne(id));
	}

	/**
	 * 服务端或者用户端更新赛事
	 * 
	 * @return
	 */
	@RequestMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(value = "更新维修表信息", notes = "update maintainTable", httpMethod = "GET", response = DwzMessage.class)
	public ResponseEntity<DwzMessage> update(MaintainTable maintainTable) {
		service.updateByPrimaryKeySelective(maintainTable);
		DwzMessage dwzMessage = new DwzMessage();
		dwzMessage.setStatusCode(200);
		dwzMessage.setMessage("更新成功");
		dwzMessage.setCallbackType("forward");
		dwzMessage.setForwardUrl("/equipment/maintianTable/maintainAllMsg");
		dwzMessage.setNavTabId("维修表详情");
		return new ResponseEntity<DwzMessage>(dwzMessage, HttpStatus.OK);
	}
}
